package com.venus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.venus.entity.Consultant;
import com.venus.entity.Student;
import com.venus.dto.StudentDto;
import com.venus.entity.Tool;
import com.venus.entity.User;
import com.venus.service.ConsultantService;
import com.venus.service.StudentService;
import com.venus.service.ToolService;
import com.venus.service.TrackService;
import com.venus.util.PageUtil;
import com.venus.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 学生表 前端控制器
 * </p>
 *
 * @author Venus
 * @since 2020-06-08
 */
@RestController
@RequestMapping("/student")
public class StudentController {

    @Resource
    private ToolService toolService;
    @Resource
    private ConsultantService conService;
    @Resource
    private StudentService studentService;
    @Resource
    private TrackService trackService;

    /**
     * 条件查询所有学生信息
     *
     * @param student
     * @param pageUtil
     * @return
     */
    @RequestMapping("/selectAllPageStudent")
    public Object selectAllPageStudent(Student student, PageUtil pageUtil) {
        //封装分页对象
        Page<Student> page = new Page<Student>(pageUtil.getPage(), pageUtil.getLimit());
        //封装条件查询
        QueryWrapper<Student> queryWrapper = new QueryWrapper<Student>();
        //isEmpty()等价于  ！=“”  是防止有字符串  但值为空
        if (student.getStuName() != null && !student.getStuName().isEmpty()) {
            queryWrapper.like("stuName", "stu_ame");
        }
        //条件和分页数据，调用分页方法查询
        page = studentService.page(page, queryWrapper);
        return new Result(0, "成功", page.getRecords(), page.getTotal());
    }

    /**
     * http://localhost:8080/selectPage 动态条件分页查
     * 使用了mybatis plus的Page<T>对象接收分页条件，
     * 前台页面laytable渲染时，请求参数修改为current和size
     *
     * @param sd 查询条件
     * @return layui数据表格要的数据格式
     *//*

    @RequestMapping("/selectAllByPage")
    public Object selectAllByPage(StudentDto sd, Page<Student> page) {
        System.out.println("emp查询条件：" + sd);
        // 出入条件和分页数据，调用分页方法查询
        IPage<Student> p = StudentService.selectStuPage(page, sd);
        //调用工具类Result，将查询结果封装成layui需要的数据
        return new Result(0, "查询成功", p.getRecords(), p.getTotal());
    }
*/
    /**
     * @param student
     * @return 0代表成功
     *//*
    @RequestMapping("/update")
    public Object update(Student student) {
        boolean bl = StudentService.updateById(student);
        if (bl) {
            return new Result(0, "修改成功！");
        }
        return new Result(1, "修改失败！");
    }

    *//**
     * @param student
     * @return 0代表成功
     *//*
    @RequestMapping("/insert")
    public Object insert(Student student) {
        System.out.println("接收到的要新增的student:" + student);
        boolean bl = StudentService.save(student);
        if (bl) {
            return new Result(0, "添加成功！");
        }
        return new Result(2, "添加失败！");
    }

    *//**
     * @param stuId
     * @return 0代表成功
     *//*
    @RequestMapping("/delete")
    public Object delete(Integer stuId) {
        boolean bl = StudentService.removeById(stuId);
        if (bl) {
            return new Result(0, "删除成功！");
        }
        return new Result(1, "删除失败！");
    }
*/
    /**
     *  查询全部学生
     * @return
     */
    /*@RequestMapping("/selectAll")
    public Object getAllStu() {
        List<Student> studentList = StudentService.list();
        System.out.println(studentList);
        return studentList;
    }*/

    /**
     *  根据网络咨询师查询学生
     * @return
     */
   /* @RequestMapping("/selectStuByUserId")
    public Object getStuByUserId(Student student) {
        List<Student> studentList = StudentService.g();
        System.out.println(studentList);
        return studentList;
    }*/
    /**
     *  根据咨询师查询学生
     * @return
     */
   /* @RequestMapping("/selectStuByUserId2")
    public Object getAllStuByUserId2(Student student) {
        List<Student> studentList = StudentService.list();
        System.out.println(studentList);
        return studentList;
    }*/


    /**
     * @param student
     * @return 0代表成功
     */
    @RequestMapping("/create")
    public Object create(Student student) {
        System.out.println("接收到的要新增的student:" + student);
        //先查询自动分配是否开启
        Tool tool = toolService.getOne(new QueryWrapper<Tool>().eq("is_auto", 1));
        //如果自动分配开启
        if (tool.getIsAuto().equals("1")) {
            //查询权重最低的 并且开启分配 的那个
            QueryWrapper<Consultant> con = new QueryWrapper<Consultant>();
            con.eq("is_open", 1);
            con.orderByAsc("con_weight");
            con.last("limit 0,1");
            Consultant consultant = conService.getOne(con);
            //获取咨询师的信息  并添加到学生信息里

            int uid = consultant.getUserId();
            String uname = consultant.getUserName();

            student.setUserId2(uid);
            student.setUserName2(uname);
            student.setUserName2(consultant.getUserName());

            boolean bl = studentService.save(student);
            if (bl) {
                //给其他咨询师增加权重
                Integer i = conService.updatecon(uid);
                if (i != 0) {
                    return new Result(0, "添加成功！");
                } else {
                    return new Result(1, "添加失败！");
                }
            } else {
                return new Result(1, "添加失败！");
            }
        } else {
            boolean bl = studentService.save(student);
            if (bl) {
                return new Result(0, "添加成功！");
            } else {
                return new Result(1, "添加失败！");
            }
        }


    }


}
